/**	
 * DroidKit is a toolkit for the Android platform
 * Copyright (C) 2012  Jonathan Bonazza
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */

package com.droidkit.net;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.Proxy;
import java.net.UnknownHostException;

import android.util.Log;

public class DefaultSocketClient extends AbstractSocketClient
{
	protected BufferedReader inStream;
	protected DataOutputStream outStream;
	
	public DefaultSocketClient(String uid, String addr, int port) throws IOException, UnknownHostException
	{
		super(uid, addr, port);
		
		inStream = new BufferedReader(new InputStreamReader(soc.getInputStream()));
		outStream = new DataOutputStream(soc.getOutputStream());
	}
	
	public DefaultSocketClient(String uid, Proxy proxy) throws IOException
	{
		super(uid, proxy);
		
		inStream = new BufferedReader(new InputStreamReader(soc.getInputStream()));
		outStream = new DataOutputStream(soc.getOutputStream());
	}
	
	public DefaultSocketClient(String uid, String addr, int port, InetAddress localAddr, int localPort) throws IOException, UnknownHostException
	{
		super(uid, addr, port, localAddr, localPort);
		
		inStream = new BufferedReader(new InputStreamReader(soc.getInputStream()));
		outStream = new DataOutputStream(soc.getOutputStream());
	}
	
	public DefaultSocketClient(String uid, InetAddress addr, int port) throws IOException
	{
		super(uid, addr, port);
		
		inStream = new BufferedReader(new InputStreamReader(soc.getInputStream()));
		outStream = new DataOutputStream(soc.getOutputStream());
	}
	
	public DefaultSocketClient(String uid, InetAddress addr, int port, InetAddress localAddr, int localPort) throws IOException
	{
		super(uid, addr, port, localAddr, localPort);
		
		inStream = new BufferedReader(new InputStreamReader(soc.getInputStream()));
		outStream = new DataOutputStream(soc.getOutputStream());
	}

	@Override
	public void sendMessage(String msg) throws IOException 
	{
		String tmp = msg + "\n";
		outStream.writeBytes(tmp);
		Log.d("DEBUG", "Sent: " + msg + " (" + tmp.getBytes().length + " bytes)");
	}

	@Override
	public String recieveMessage() throws IOException 
	{
		 String msg = inStream.readLine();
		 
		 if(msg != null)
			 Log.d("DEBUG", "Recieved: " + msg + " (" + msg.getBytes().length + "bytes)");
		 
		 return msg;
	}
	
	public void flush() throws IOException
	{
		outStream.flush();
	}
	
	public void expect(String str) throws IOException
	{
		if(!recieveMessage().startsWith(str))
		{
			Log.e("ERROR", "ERROR: Expected Response Not Found");
			throw new IOException();
		}
	}
}
